﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using Yipusoft.Common.BLL;
using Yipusoft.Common.Model;
using Yipusoft.Task.BLL;
using Yipusoft.Task.Model;
using Yipusoft.Evaluate.BLL;
using Yipusoft.Evaluate.Model;
using System.Text;
using System.Data;

public partial class Evaluate_BaseReportDatachment : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, "100103"))
        {
        }

        if (!IsPostBack)
        {
            BindYearList();
            CreateTable();

        }

    }
    private void BindYearList()
    {
        for (int i = 2008; i <= 2050; i++)
        {
            ListItem li = new ListItem();
            li.Text = i.ToString() + "年";
            li.Value = i.ToString();
            YearList.Items.Add(li);
        }
        YearList.SelectedValue = DateTime.Now.Year.ToString();

    }
    private void CreateTable()
    {
        string departmentId = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        DataSet ds = SplitPage.ExecuteSQL("SELECT DepartmentID, ShortName FROM Account_Departments WHERE departmentId like '" + departmentId.Substring(0, 4) + rbType.SelectedValue + "%'  Group By DepartmentID, ShortName");
        StringBuilder sql = new StringBuilder();
        sql.AppendLine("DECLARE @SQL nvarchar(4000)");
        sql.AppendLine("SET @SQL = ''");
        sql.AppendLine("SELECT @SQL = @SQL + ', SUM(CASE WHEN b.DepartmentId = ''' + DepartmentID + ''' THEN b.ItemScore END) AS D' + DepartmentID FROM Account_Departments WHERE departmentId like '" + departmentId.Substring(0, 4) + rbType.SelectedValue + "%' Group By DepartmentID");
        sql.AppendLine("SET @SQL = 'SELECT a.BaseID, a.BaseName' + @SQL + ' FROM Base a LEFT JOIN BaseItems b ON b.BaseID = a.BaseID  WHERE Unitid like ''" + departmentId.Substring(0, 6) + "%'' and Flag=" + rbType.SelectedValue + " and (b.YearValue=" + YearList.SelectedValue + " or b.YearValue is null)  GROUP BY a.BaseID, a.BaseName'");
        sql.AppendLine("EXEC(@SQL)");
        DataView dv = SplitPage.ExecuteSQL(sql.ToString()).Tables[0].DefaultView;

        Table tb = new Table();
        tb.Caption = "<span>" + YearList.SelectedItem.Text + "基础数据评估得分情况统计表</span>";
        tb.CellPadding = 4;
        tb.CellSpacing = 0;
        TableRow tr = new TableRow();
        TableCell tc = new TableCell();
        tr.CssClass = "grid_header";

        tc.Text = "";

        tr.Cells.Add(tc);
        tc = new TableCell();
        tc.HorizontalAlign = HorizontalAlign.Left;
        tc.BackColor = System.Drawing.Color.LightGray;
        tc.Text = "项目名称";
        tc.Style.Add("white-space", "nowrap");
        tr.Cells.Add(tc);

        int col = ds.Tables[0].Rows.Count;

        for (int i = 0; i < col; i++)
        {
            tc = new TableCell();
            tc.HorizontalAlign = HorizontalAlign.Right;

            tc.Style.Add("writing-mode", "tb-lr");
            tc.Style.Add("white-space", "nowrap");
            tc.Text = ds.Tables[0].Rows[i][1].ToString();
            tr.Cells.Add(tc);
            tb.Rows.Add(tr);
        }
        int row = 1;
        foreach (DataRowView drv in dv)
        {
            BaseInfo baseinfo = Base.GetBase((int)drv[0]);
            decimal singleScore = baseinfo.TotalScore * 12;
            tr = new TableRow();
            tr.CssClass = "grid_item";
            tc = new TableCell();
            tc.HorizontalAlign = HorizontalAlign.Center;
            tc.Text = row.ToString();
            tr.Cells.Add(tc);
            tc = new TableCell();
            tc.BackColor = System.Drawing.Color.White;
            tc.Text = baseinfo.BaseName;
            tc.Style.Add("white-space", "nowrap");
            tr.Cells.Add(tc);
            for (int i = 2; i <= col + 1; i++)
            {
                tc = new TableCell();
                tc.BackColor = System.Drawing.Color.White;
                tc.HorizontalAlign = HorizontalAlign.Center;
                string score = "";
                if (drv[i] != DBNull.Value)
                    score = (singleScore - (decimal)drv[i]).ToString("0");
                else
                    score = singleScore.ToString("0");
                tc.Text = "<a href=BaseViewD.aspx?BaseId=" + drv[0].ToString() + "&DepartmentId=" + dv.Table.Columns[i].ColumnName.Replace("D", "") + "&Year=" + YearList.SelectedValue + ">" + score + "</a>";
                tr.Cells.Add(tc);
            }
            tb.Rows.Add(tr);
            row++;
        }

        string sql1 = "unitid like '" + departmentId.Substring(0, 6) + "%' and flag=" + rbType.SelectedValue + " ";
        decimal totalScore = Base.GetList(sql1).Sum(o => o.TotalScore) * 12;
        //添加合计列
        sql = new StringBuilder();
        sql.AppendLine("DECLARE @SQL nvarchar(4000)");
        sql.AppendLine("SET @SQL = ''");
        sql.AppendLine("SELECT @SQL = @SQL + ', SUM( CASE WHEN b.DepartmentId = ''' + DepartmentID + ''' THEN b.ItemScore END)' FROM Account_Departments WHERE departmentId like '" + departmentId.Substring(0, 4) + rbType.SelectedValue + "%' Group By DepartmentID");
        sql.AppendLine("SET @SQL = 'select 0 '+@SQL+' from baseitems b where baseid in (select baseid from base where left(Unitid,6)=''" + departmentId.Substring(0, 6) + "'' and Flag=" + rbType.SelectedValue + ") and YearValue=" + YearList.SelectedValue + "'");
        sql.AppendLine("EXEC(@SQL)");
        dv = SplitPage.ExecuteSQL(sql.ToString()).Tables[0].DefaultView;
        tr = new TableRow();
        tr.CssClass = "grid_item";
        tc = new TableCell();
        tc.Text = "";
        tc.Width = Unit.Pixel(30);
        tr.Cells.Add(tc);
        tc = new TableCell();
        tc.HorizontalAlign = HorizontalAlign.Left;
        tc.Text = "合计";
        tr.Cells.Add(tc);
        for (int i = 1; i <= col; i++)
        {
            tc = new TableCell();
            tc.HorizontalAlign = HorizontalAlign.Center;
            if (dv[0][i] != DBNull.Value)
            {
                tc.Text = (totalScore - decimal.Parse(dv[0][i].ToString())).ToString("0");
            }
            else
            {
                tc.Text = totalScore.ToString("0");
            }
            tr.Cells.Add(tc);
            tb.Rows.Add(tr);
        }
        PlaceHolder.Controls.Add(tb);

        BindChart();
    }
    private void BindChart()
    {
        string departmentId = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        string sql1 = "unitid like '" + departmentId.Substring(0, 6) + "%' and flag=" + rbType.SelectedValue + " ";
        decimal totalScore = Base.GetList(sql1).Sum(o => o.TotalScore) * 12;
        StringBuilder sql = new StringBuilder();
        sql.Append("select  DepartmentID,shortName, ");
        sql.Append("(select " + totalScore + "-case when  sum(itemscore) is null then 0 else sum(itemscore) end  from Baseitems where Baseid in (select Baseid from Base where left(Unitid,6)='" + departmentId.Substring(0, 6) + "' and flag=" + rbType.SelectedValue + " and  YearValue=" + YearList.SelectedValue + ") and DepartmentID=account_departments.departmentid)  as score ");
        sql.Append("from account_departments where departmentId like '" + departmentId.Substring(0, 4) + rbType.SelectedValue + "%' Group by DepartmentID,shortName order by score desc");
        DataView dv = SplitPage.ExecuteSQL(sql.ToString()).Tables[0].DefaultView;
        UserChart.Titles["Title"].Text = YearList.SelectedItem.Text + "各单位基础数据评估得分情况统计表";
        UserChart.ChartAreas["Default"].AxisX.LabelsAutoFit = false;
        dv.Sort = " Score asc ";
        UserChart.Series["得分"].Points.DataBindXY(dv, "shortName", dv, "Score");
        UserChart.Series[0].Type = Dundas.Charting.WebControl.SeriesChartType.Bar;
        UserChart.Height = 1000;
    }
    protected void YearList_SelectedIndexChanged(object sender, EventArgs e)
    {
        CreateTable();
    }
    protected void UserChart_DataBound(object sender, EventArgs e)
    {

    }
    protected void rbType_SelectedIndexChanged(object sender, EventArgs e)
    {
        CreateTable();
    }
}
